/**
 * Copyright By Nanjing Fujitsu Nanda Software Technology Co., Ltd
 * 下午6:28:13
 * DateUtils.java
 * 
 */
package com.snda.game.legend.base;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author qixb.fnst
 * 
 */
public class DateUtils {

    private static Calendar calendar = Calendar.getInstance();
    private static DateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    public static Date getBeginTimeOfDay(Date date) {
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static String format(Date date) {
        if (date == null) {
            date = new Date();
        }
        return dateFormat.format(date);
    }

    public static Date getBeginTimeOfNextDay(Date date) {
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
        return calendar.getTime();
    }

    public static Date getEndTime(Date openTime) {
        calendar.setTime(openTime);
        boolean result = calendar.get(Calendar.MINUTE) >= 30;
        calendar.add(Calendar.MINUTE, 30);
        calendar.set(Calendar.SECOND, 0);
        if (result) {

            calendar.set(Calendar.MINUTE, 0);
        } else {
            calendar.set(Calendar.MINUTE, 30);
        }
        return calendar.getTime();
    }

    /**
     * 获取之前没有被更新的时间
     * */
    public static Date getBeginTime() {
        calendar.setTime(new Date());
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getEndTime() {
        return getEndTime(new Date());
    }

    public static Date getThreeDaysBeforeTime(Date now) {
        calendar.setTime(now);
        calendar.add(Calendar.DAY_OF_WEEK_IN_MONTH, -3);
        return calendar.getTime();
    }

    public static Date getThreeDaysAfterTime(Date now) {
        calendar.setTime(now);
        calendar.add(Calendar.DAY_OF_WEEK_IN_MONTH, 3);
        return calendar.getTime();
    }
}
